<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:p="http://primefaces.org/ui"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:o="http://omnifaces.org/ui"
	xmlns:cc="http://java.sun.com/jsf/composite">

<cc:interface>
	<cc:attribute name="columns" required="true" shortDescription="Colunas que serão exibidas no modal." />
	<cc:attribute name="filters" required="false" shortDescription="Filtros da consulta." />
	<cc:attribute name="fields" required="false" shortDescription="Campos para Inclusão/Edição." />
	<cc:attribute name="header" required="true" />
	<cc:attribute name="height" required="false" />
	<cc:attribute name="id" required="true" />
	<cc:attribute name="list" required="false" />
	<cc:attribute name="modalAction" required="true" />
	<cc:attribute name="onClose" required="false" method-signature="void action()" />
	<cc:attribute name="rows" required="false" />
	<cc:attribute name="selection" required="true" />
	<cc:attribute name="selectionMode" required="false" />
	<cc:attribute name="selectionType" required="false" />	
	<cc:attribute name="update" required="true" />
	<cc:attribute name="width" required="false" />
</cc:interface>

<cc:implementation>

	<script type="text/javascript">
		function onShowModal() {
			//$("div.ui-datatable-header").css("width", "270px");
		}

		function onHideModal() {
			//$("div.ui-datatable-header").css("width", "955px");
			//$("div.ui-datatable-header").css("float", "none");
		}	
	</script>
	
	<p:dialog
		id="dialog#{cc.attrs.id}"
		showEffect="fade" hideEffect="explode"
		onShow="onShowModal();" onHide="onHideModal();" widgetVar="dialogModal#{cc.attrs.id}" modal="true"
		height="#{cc.attrs.height != null ? cc.attrs.height : '530'}" width="#{cc.attrs.width != null ? cc.attrs.width : '584'}"
		closeOnEscape="true">
		<!-- Para Atualizar o lista da tela que chama o modal, onde será inserido os registros quando for um modal de edicao -->
		<!-- <p:ajax event="close" update=":formCrud:panelEdit" /> -->
	
		<h:form id="formModal#{cc.attrs.id}">
		
			<p:ajaxStatus>
				<f:facet name="start">
					<div class="carregando" />
				</f:facet>
			</p:ajaxStatus>
					
			<h:panelGroup id="panelListModal#{cc.attrs.id}" layout="block">
				<h:panelGroup layout="block" rendered="#{cc.attrs.filters != null and cc.attrs.columns != null}">
				
					<p:panel id="panelFiltersModal#{cc.attrs.id}" widgetVar="panelFiltersModal#{cc.attrs.id}" header="#{cc.attrs.header}">
						<p:focus context="panelFiltersModal#{cc.attrs.id}"/>
						
						<ui:include src="#{cc.attrs.filters}">
							<ui:param name="modal" value="true" />
						</ui:include>
						
						<br clear="all" />
						<br clear="all" />
					</p:panel>
					
					<div class="buttonList">
						<p:commandButton id="cbSearchModal#{cc.attrs.id}" value="#{messages['button.search']}" action="#{cc.attrs.list.find()}" update="panelDataTableModal#{cc.attrs.id}"
							onclick="upper()"							
							oncomplete="onShowModal();"							
							partialSubmit="true"
							process="@this @widgetVar(panelFiltersModal#{cc.attrs.id})">
							<o:validateBean disabled="true" />
						</p:commandButton>
						<span class="pipe">|</span>
						<p:commandLink action="#{cc.attrs.list.clearInstance()}" value="#{messages['button.clean']}" update="panelListModal#{cc.attrs.id}">
							<o:validateBean disabled="true" />
							<p:resetInput target="panelFiltersModal#{cc.attrs.id}" />
						</p:commandLink>
						<p:defaultCommand target="cbSearchModal#{cc.attrs.id}" />
					</div>
					
					<br clear="all" />
					
					<h:panelGroup id="panelDataTableModal#{cc.attrs.id}" layout="block">
						<div style="overflow-y: scroll; overflow-x: hidden; height: 315px;">
						<p:dataTable
							id="dataTableModal#{cc.attrs.id}" value="#{cc.attrs.list.pagedResult}" lazy="true" var="entity" emptyMessage="" rendered="#{cc.attrs.list.hasResult()}"
							selection="#{cc.attrs.list.selections}" rowKey="#{entity.id}"
							selectionMode="#{cc.attrs.selectionMode != null ? cc.attrs.selectionMode : 'multiple'}" rows="#{cc.attrs.rows != null ? cc.attrs.rows : 20}"
							paginator="true" paginatorPosition="top"
							paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}">				    
					        
					        <p:ajax event="rowSelect" update="dataTableModal#{cc.attrs.id}:panelButtons" immediate="true" partialSubmit="true" />
					        <p:ajax event="rowUnselect" update="dataTableModal#{cc.attrs.id}:panelButtons" immediate="true" partialSubmit="true" />
					        
					        <f:facet name="header">
					        	<h:panelGroup id="panelButtons">
							        <p:commandButton
							        	rendered="#{cc.attrs.selectionType != null}"
							        	value="#{messages['button.select']}" action="#{cc.attrs.selection.addAll(cc.attrs.list.selections)}"
							        	update="#{cc.attrs.update}" oncomplete="onHideModal();PF('dialogModal#{cc.attrs.id}').hide();"
							        	disabled="#{!cc.attrs.list.hasSelection()}"
							        	partialSubmit="true" process="@this" />
							        <p:commandButton
							        	rendered="#{cc.attrs.selectionType == null and cc.attrs.onClose == null}"
							        	value="#{messages['button.select']}"
							        	update="#{cc.attrs.update}" oncomplete="onHideModal();PF('dialogModal#{cc.attrs.id}').hide();"
							        	disabled="#{!cc.attrs.list.hasSelection()}"
							        	partialSubmit="true" process="@this">
							        	<f:setPropertyActionListener target="#{cc.attrs.selection}" value="#{cc.attrs.list.selections.get(0)}" />
							        </p:commandButton>
							        <p:commandButton						        	
							        	action="#{cc.attrs.onClose}"
							        	rendered="#{cc.attrs.selectionType == null and cc.attrs.onClose != null}"
							        	value="#{messages['button.select']}"
							        	update="#{cc.attrs.update}" oncomplete="onHideModal();PF('dialogModal#{cc.attrs.id}').hide();"
							        	disabled="#{!cc.attrs.list.hasSelection()}"
							        	partialSubmit="true" process="@this">
							        	<f:setPropertyActionListener target="#{cc.attrs.selection}" value="#{cc.attrs.list.selections.get(0)}" />
							        </p:commandButton>
						        </h:panelGroup>
					        </f:facet>
					        
					        <ui:include src="#{cc.attrs.columns}">
					        	<ui:param name="modal" value="true" />
					        </ui:include>
					        
						</p:dataTable>
						</div>
					</h:panelGroup>
					
				</h:panelGroup>
			</h:panelGroup>
			
			<h:panelGroup id="panelEditModal#{cc.attrs.id}" layout="block">
					
				<h:panelGroup layout="block" rendered="#{cc.attrs.fields != null and cc.attrs.filters == null}">
	
					<p:panel id="panelFields#{cc.attrs.id}" widgetVar="panelFields#{cc.attrs.id}" header="#{cc.attrs.header}">
						<p:focus context="panelFields#{cc.attrs.id}"/>
						<ui:include src="#{cc.attrs.fields}">
							<ui:param name="modal" value="true" />
						</ui:include>						
						<br clear="all" />
						<br clear="all" />
					</p:panel>
					
					<div class="buttonEdit">			
					
						<p:commandButton
							onclick="upper();"
							actionListener="#{cc.attrs.selection.add(cc.attrs.modalAction.instance)}"
							oncomplete="if (!args.validationFailed) {onHideModal();PF('dialogModal#{cc.attrs.id}').hide();}"
							value="#{messages['button.save']}"				
							update="panelFields#{cc.attrs.id} #{cc.attrs.update}"
							partialSubmit="true" process="@this @widgetVar(panelFields#{cc.attrs.id})"
							rendered="#{!cc.attrs.modalAction.managed and !cc.attrs.modalAction.editModal and cc.attrs.onClose == null}">
							<p:resetInput target="panelFields#{cc.attrs.id}" />
						</p:commandButton>
						
						<p:commandButton
							onclick="upper();"
							action="#{cc.attrs.onClose}"
							actionListener="#{cc.attrs.selection.add(cc.attrs.modalAction.instance)}"
							oncomplete="if (!args.validationFailed) {onHideModal();PF('dialogModal#{cc.attrs.id}').hide();}"
							value="#{messages['button.save']}"				
							update="panelFields#{cc.attrs.id} #{cc.attrs.update}"
							partialSubmit="true" process="@this @widgetVar(panelFields#{cc.attrs.id})"
							rendered="#{!cc.attrs.modalAction.managed and !cc.attrs.modalAction.editModal and cc.attrs.onClose != null}">
							<p:resetInput target="panelFields#{cc.attrs.id}" />
						</p:commandButton>						

						<p:commandButton
							onclick="upper();"
							oncomplete="if (!args.validationFailed) {onHideModal();PF('dialogModal#{cc.attrs.id}').hide();}"
							value="#{messages['button.save']}"					
							update="panelFields#{cc.attrs.id} #{cc.attrs.update}"
							partialSubmit="true" process="@this @widgetVar(panelFields#{cc.attrs.id})"							
							rendered="#{(cc.attrs.modalAction.managed or cc.attrs.modalAction.editModal) and cc.attrs.onClose == null}">
							<p:resetInput target="panelFields#{cc.attrs.id}" />
						</p:commandButton>
						
						<p:commandButton
							onclick="upper();"
							action="#{cc.attrs.onClose}"
							oncomplete="if (!args.validationFailed) {onHideModal();PF('dialogModal#{cc.attrs.id}').hide();}"
							value="#{messages['button.save']}"					
							update="panelFields#{cc.attrs.id} #{cc.attrs.update}"
							partialSubmit="true" process="@this @widgetVar(panelFields#{cc.attrs.id})"							
							rendered="#{(cc.attrs.modalAction.managed or cc.attrs.modalAction.editModal) and cc.attrs.onClose != null}">
							<p:resetInput target="panelFields#{cc.attrs.id}" />
						</p:commandButton>						
												
						<span class="pipe">|</span>
						
						<p:commandLink
							oncomplete="onHideModal();PF('dialogModal#{cc.attrs.id}').hide();"
							value="#{messages['link.cancel']}" partialSubmit="true" process="@this" immediate="true">
						</p:commandLink>
					</div>
					
				</h:panelGroup>					
						
			</h:panelGroup>
		</h:form>
	</p:dialog>
</cc:implementation>
</html>